package com.ruoyi.operation.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.operation.cmdb.CmdbConcurrenHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.operation.domain.vo.TBusisystemVo;
import com.ruoyi.operation.domain.bo.TBusisystemBo;
import com.ruoyi.operation.service.ITBusisystemService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;





import org.dromara.warm.flow.core.enums.ConditionType;

/**
 * 业务系统，从CMDB同步控制器
 * 前端访问路由地址为:/operation/busisystem
 *
 * @author chq
 * @date 2025-06-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/busisystem")
@Slf4j
public class TBusisystemController extends BaseController {

    private final ITBusisystemService iTBusisystemService;

    private final CmdbConcurrenHelper cmdbConcurrenHelper;

    /**
     * 查询业务系统，从CMDB同步列表
     */
    @SaCheckPermission("operation:busisystem:list")
    @GetMapping("/list")
    public TableDataInfo<TBusisystemVo> list(TBusisystemBo bo, PageQuery pageQuery) {
        return iTBusisystemService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出业务系统，从CMDB同步列表
     */
    @SaCheckPermission("operation:busisystem:export")
    @Log(title = "业务系统，从CMDB同步", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TBusisystemBo bo, HttpServletResponse response) {
        List<TBusisystemVo> list = iTBusisystemService.queryList(bo);
        ExcelUtil.exportExcel(list, "业务系统，从CMDB同步", TBusisystemVo.class, response);
    }

    /**
     * 获取业务系统，从CMDB同步详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("operation:busisystem:query")
    @GetMapping("/{id}")
    public R<TBusisystemVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iTBusisystemService.queryById(id));
    }

    /**
     * 新增业务系统，从CMDB同步
     */
    @SaCheckPermission("operation:busisystem:add")
    @Log(title = "业务系统，从CMDB同步", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TBusisystemBo bo) {
        return toAjax(iTBusisystemService.insertByBo(bo));
    }

    /**
     * 修改业务系统，从CMDB同步
     */
    @SaCheckPermission("operation:busisystem:edit")
    @Log(title = "业务系统，从CMDB同步", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TBusisystemBo bo) {
        return toAjax(iTBusisystemService.updateByBo(bo));
    }

    /**
     * 删除业务系统，从CMDB同步
     *
     * @param ids 主键串
     */
    @SaCheckPermission("operation:busisystem:remove")
    @Log(title = "业务系统，从CMDB同步", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iTBusisystemService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @GetMapping("/concurrent")
//    @SaCheckPermission("operation:busisystem:concurrent")
    public R<?> concurrentByCMDB(@RequestParam Map<String,Object> queryParam) {
        if (queryParam.isEmpty()) {
            return R.fail("参数不能为空");
        }
        if (Objects.isNull(queryParam.get("q"))){
            return R.fail("查询参数不能为空");
        }
        List<Map<String, String>> resultData = cmdbConcurrenHelper.getCMDBData(queryParam);
//        String path = "/Users/ozzo/program/ab/ruoyi-cloud-plus/ruoyi-modules/ruoyi-operation/src/main/java/com/ruoyi/operation/controller/t_busisystem.json";
//        try {
//            String jsonContent = new String(Files.readAllBytes(Paths.get(path)));
//            JSONObject jsonObject = JSONUtil.parseObj(jsonContent);
//            resultData = (List<Map<String, String>>) jsonObject.get("result");
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
        log.info("cmdb结果数据获得为{}",resultData);
        return toAjax(iTBusisystemService.currentCMDB(resultData));
    }




}
